Jsoncpp如何使用及样例

您所在的位置:网站首页 json fastwriter Jsoncpp如何使用及样例

Jsoncpp如何使用及样例

#Jsoncpp如何使用及样例| 来源: 网络整理| 查看: 265

下载对应的版本

下载:https://github.com/184622608/jsoncpp 注意:主要两个大的版本,一个支持C++11, 一个不支持 1.y.z is built with C++11. 0.y.z can be used with older compilers. 我公司这里使用的是旧的不运行C++11的编译器里使用,所以只能选择0.y.z的版本下载

如何在项目中使用

我这里是直接使用源码配合使用做成其它功能,然后编译成动态库

1,生成合并的头和源文件

它这里是使用pthon来生成一个合并的头文件与源文件(this requires Python 2.6): python amalgamate.py

It is possible to specify header name. See the -h option for detail.

By default, the following files are generated:

1. dist/jsoncpp.cpp: source file that needs to be added to your project. 2. dist/json/json.h: corresponding header file for use in your project. It is equivalent to including json/json.h in non-amalgamated source. This header only depends on standard headers. 3. dist/json/json-forwards.h: header that provides forward declaration of all JsonCpp types.

The amalgamated sources are generated by concatenating JsonCpp source in the correct order and defining the macro JSON_IS_AMALGAMATION to prevent inclusion of other headers.

2,在项目使用

你的生成目录\jsoncpp\jsoncpp-0.y.z\dist\

1,将dist目录下的 json头文件目录 和 json.cpp 拷贝到 你的项目里 2,在你的项目中属性 >> 配置属性 >> C/C++ >> 附加包含目录 包含 json 目录 3,在你想使用jsoncpp的源文件中包含头文件 #include "json/json.h" 4,解析json数据 例1:

要解析的数据如下:

{"retCode":"0000","retMsg":"成功", "keyCurVerList": [{ "issueChannelCode":"01","keyId":"01","keyBathNumber":"2","needUpdateYN":"Y", "keyList": [ {"keyBathNumber":"2","keyIdx":"01","keyValue":"1111111111","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"02","keyValue":"2222222222","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"03","keyValue":"3333333333","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"04","keyValue":"4444444444","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"05","keyValue":"5555555555","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"06","keyValue":"6666666666","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"07","keyValue":"7777777777","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"08","keyValue":"8888888888","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"09","keyValue":"9999999999","keyEffectiveDate":"20271101"}, {"keyBathNumber":"2","keyIdx":"0A","keyValue":"AAAAAAAAAA","keyEffectiveDate":"20271101"} ] }] } // 根据要解析的数据定义相对应的结构体 struct ST_Key { std::string keyBathNumber; std::string keyIdx; std::string keyValue; std::string keyEffectiveDate; }; struct ST_KeyCurVer { std::string issueChannelCode; std::string keyId; std::string keyBathNumber; std::string needUpdateYN; std::vector keyList; }; struct ST_KeySync_Ret { std::string retCode; std::string retMsg; std::vector keyCurVerList; }; // 使用方法 // 接收到要解析的json内容存储在 string revc_text 中 Json::Reader reader; Json::Value value; if (reader.parse(recv_text, value)) //revc_text要解析的json文本 { ret.retCode = value["retCode"].asString(); //ST_KeySync_Ret ret为本地要存储json数据定义的结构体 ret.retMsg = value["retMsg"].asString(); const Json::Value arrayObj = value["keyCurVerList"]; for (int i = 0; i < arrayObj.size(); ++i) { ST_KeyCurVer keyCurVer; //自定义的结构体 keyCurVer.issueChannelCode = arrayObj[i]["issueChannelCode"].asString(); keyCurVer.keyId = arrayObj[i]["keyId"].asString(); keyCurVer.keyBathNumber = arrayObj[i]["keyBathNumber"].asString(); keyCurVer.needUpdateYN = arrayObj[i]["needUpdateYN"].asString(); const Json::Value subArrayObj = arrayObj[i]["keyList"]; for (int j = 0; j < subArrayObj.size(); ++j) { ST_Key key; //自定义的结构体 key.keyBathNumber = subArrayObj[j]["keyBathNumber"].asString(); key.keyIdx = subArrayObj[j]["keyIdx"].asString(); key.keyValue = subArrayObj[j]["keyValue"].asString(); key.keyEffectiveDate = subArrayObj[j]["keyEffectiveDate"].asString(); keyCurVer.keyList.push_back(key); } ret.keyCurVerList.push_back(keyCurVer); } } 例2: // 要解析的json文本 // {"retCode":"0000","retMsg":"成功","adviceOpt":["006"],"managerCode":"","transAmount":"0"} Json::Reader reader; Json::Value value; if (reader.parse(recv_text, value)) //revc_text要解析的json文本 { ret.retCode = value["retCode"].asString(); ret.retMsg = value["retMsg"].asString(); ret.managerCode = value["managerCode"].asString(); ret.transAmount = value["transAmount"].asString(); const Json::Value arrayObj = value["adviceOpt"]; for (int i = 0; i < arrayObj.size(); ++i) { ret.adviceOpt = arrayObj[i].asString(); } } 生成Json数据字符串 /* { "device_id":"3984723987923784936", "mobile":"13020149283", "device_type":"android", "account_id_hash":"123456789", "source_ip":"111.111.111.111" } */ m_risk.insert(std::make_pair("device_id", data.riskinfo.device_id)); m_risk.insert(std::make_pair("mobile", data.riskinfo.mobile)); m_risk.insert(std::make_pair("device_type", data.riskinfo.device_type)); m_risk.insert(std::make_pair("account_id_hash", data.riskinfo.account_id_hash)); m_risk.insert(std::make_pair("source_ip", data.riskinfo.source_ip)); Json::Value root; std::map::iterator it = m_risk.begin(); for (;it != m_risk.end(); ++it){ root[it->first] = it->second; } Json::FastWriter writer; std::string str_risk = writer.write(root); /* {"account_id_hash":"123456789","device_id":"3984723987923784936","device_type":"android","mobile":"13020149283","source_ip":"111.111.111.111"} */ // 如果想要排列好看的字符串 std::string str_risk_styled= root.toStyledString(); /* { "account_id_hash" : "123456789", "device_id" : "3984723987923784936", "device_type" : "android", "mobile" : "13020149283", "source_ip" : "111.111.111.111" } */ 进一步的了解 Json::Value

Json::Value 用来表示Json中的任何一种value抽象数据类型,具体来说,Json中的value可以是一下数据类型:

有符号整数 signed integer [range: Value::minInt - Value::maxInt]无符号整数 unsigned integer (range: 0 - Value::maxUInt)双精度浮点数 double字符串 UTF-8 string布尔型 boolean空 ‘null’一个Value的有序列表 an ordered list of Valuecollection of name/value pairs (javascript object)

可以通过 [ ] 下标的方法来取值。

//Examples: Json::Value null_value; // null Json::Value arr_value(Json::arrayValue); // [] Json::Value obj_value(Json::objectValue); // {} Json::Reader

Json::Reader可以通过对Json源目标进行解析,得到一个解析好了的Json::Value,通常字符串或者文件输入流可以作为源目标。

假设现在有一个example.json文件

{ "encoding" : "UTF-8", "plug-ins" : [ "python", "c++", "ruby" ], "indent" : { "length" : 3, "use_space": true } } 使用Json::Reader对Json文件进行解析 bool parse (const std::string &document, Value &root, bool collectComments=true) bool parse (std::istream &is, Value &root, bool collectComments=true) // 使用方法 Json::Value root; Json::Reader reader; std::ifstream ifs("example.json");//open file example.json if(!reader.parse(ifs, root)){ // fail to parse } else{ // success std::cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3